


// Define an interface for the request body
import { PageInstanceVO } from "@/api/type";

interface TrademarkVO {
  id: number;
  tmName: string;
  logoUrl: string;
}

const trademarks: TrademarkVO[] = [
  { id: 1, tmName: '小米', logoUrl: 'https://images.dog.ceo/breeds/pembroke/n02113023_16112.jpg' },
  { id: 2, tmName: '华为', logoUrl: 'https://images.dog.ceo/breeds/pembroke/n02113023_3471.jpg' },
  { id: 3, tmName: '苹果', logoUrl: 'https://images.dog.ceo/breeds/pembroke/n02113023_6015.jpg' },
  { id: 4, tmName: '三星', logoUrl: 'https://images.dog.ceo/breeds/pembroke/n02113023_1144.jpg' },
  { id: 5, tmName: '谷歌', logoUrl: 'https://images.dog.ceo/breeds/pembroke/n02113023_856.jpg' },
  { id: 6, tmName: '亚马逊', logoUrl: 'https://images.dog.ceo//breeds//pembroke//n02113023_3840.jpg' },
  { id: 7, tmName: '微软', logoUrl: 'https://images.dog.ceo//breeds//pembroke//n02113023_2054.jpg' },
  { id: 8, tmName: '特斯拉', logoUrl: 'https://images.dog.ceo/breeds/pembroke/n02113023_1482.jpg' }
];
// Function to simulate trademark list fetching with pagination
function queryTrademarkList(page: number, limit: number): PageInstanceVO<TrademarkVO> {
  const pageIndex = Number(page);
  const pageSize = Number(limit);
  // Calculate start and end indices for pagination
  console.log('pageIndex:', pageIndex, 'pageSize:', pageSize);

  const start = (pageIndex - 1) * pageSize;
  console.log('limit:', limit);
  //   Calculate end index for pagination

  const end = start + pageSize; // 这里吗有问题帮我修复一下
  console.log('start:', start, 'end:', end);
  return {
    list: trademarks.slice(start, end),
    total: trademarks.length,
    size: limit,
    current: page,
    searchCount: true,
    pages: Math.ceil(trademarks.length / limit),
  };
}

function addTrademark(trademark: TrademarkVO): void {
  // Simulate adding a trademark
  console.log('Adding trademark:', trademark);
  let ids = trademarks.map(item => item.id);
  if (ids.includes(trademark.id)) {
    console.log('Trademark ID already exists:', trademark.id);
    return;
  }
  trademark.id = Math.max(...ids) + 1; // Assign a new ID
  trademarks.push(trademark);
}
function updateTrademark(trademark: TrademarkVO): void {
  // Simulate updating a trademark
  console.log('Updating trademark:', trademark);
  const index = trademarks.findIndex(item => item.id === trademark.id);
  if (index !== -1) {
    trademarks[index] = trademark;
  } else {
    console.log('Trademark not found:', trademark.id);
  }
}

export default [
  {
    url: "/api/admin/product/baseTrademark/list",
    method: "get",
    response: () => {
      return {
        code: 200,
        data: trademarks,
        message: "success",
      };
    },
  },
  {
    url: "/api/admin/product/baseTrademark/list/:page/:limit",
    method: "get",
    response: ({ query: { page, limit } }: { query: { page: number, limit: number } }) => {
      if (page <= 0 || limit <= 0) {
        return {
          code: 400,
          message: "分页参数无效",
          data: null
        };
      }
      // 查询分页数据
      const paginatedData = queryTrademarkList(page as number, limit as number);
      return {
        code: 200,
        message: "success",
        data: paginatedData
      };
    }
  },

  {
    url: "/api/admin/product/baseTrademark/save",
    method: "post",
    response: ({ body }: { body: TrademarkVO }) => {
      // Simulate adding a trademark
      addTrademark(body);
      return {
        code: 200,
        message: "success"
      };
    }
  },
  {
    url: "/api/admin/product/baseTrademark/update",
    method: "put",
    response: ({ body }: { body: TrademarkVO }) => {
      // Simulate adding a trademark
      updateTrademark(body);
      return {
        code: 200,
        message: "success"
      };
    }
  },
  {
    url: "/api/admin/product/baseTrademark/remove/:id",
    method: "delete",
    response: ({ query }: { query: { id: number } }) => {
      const id = Number(query.id);
      const index = trademarks.findIndex(item => item.id === id);
      if (index !== -1) {
        trademarks.splice(index, 1);
        return {
          code: 200,
          message: "success"
        };
      } else {
        return {
          code: 404,
          message: "Trademark not found"
        };
      }
    }
  }
  ,



] 